package com.tsd.anon;

import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.VersionConstants;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.service.SysSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 描述：匿名读取数据的控制器
 * 未登录时需要获取数据的方法都写这里
 *
 * @author david
 * @date 2021/07/15 10:35:26
 */
@Controller
@Scope("prototype")
@RequestMapping("/anon")
@Api(tags = "匿名读取数据", description = "匿名读取数据的控制器")
public class AnonSettingController extends BaseController {

    @Resource
    private SysSettingService sysSettingService;


    @ApiOperation(value = "根据code和owner_sid获取设置（json）", notes = "根据id获取设置（json）")
    @ResponseBody
    @RequestMapping(value = "/getSysSettingByCodeOwnerId.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysSettingByCodeOwnerId4Anon() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysSettingExt query = super.getParams(request, SysSettingExt.class);
            super.checkEmpty(query.getCode(), "缺少code");
            SysSettingExt sysSetting = sysSettingService.getSysSettingByCodeOwnerSid(query);
            if (sysSetting == null) {
                sysSetting = query;
            }
            hr.setData(sysSetting);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取版本信息", notes = "获取版本信息")
    @ResponseBody
    @RequestMapping(value = "/queryVersionMap.action", method = RequestMethod.POST)
    public HttpResult<Object> queryVersionMap() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            hr.setData(VersionConstants.getVersionValueMap());
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
